package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.DywhExcelListener;
import cn.stylefeng.guns.sys.core.listener.ElmExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDywh;
import cn.stylefeng.guns.sys.modular.system.entity.BtyElm;
import cn.stylefeng.guns.sys.modular.system.entity.Yybb;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyElmMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDywhParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyElmParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyDywhResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyElmResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyElmService;
import cn.stylefeng.guns.sys.modular.system.service.YybbService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 饿了么账单控制器
 * @author liuy
 * @Date 2021-08-30 16:53:15
 */
@Controller
@RequestMapping("/btyElm")
public class BtyElmController extends BaseController {

    private String PREFIX = "/modular/system/btyElm";

    @Autowired
    private BtyElmService btyElmService;
    @Resource
    private BtyElmMapper elmMapper;
    @Resource
    private YybbService yybbService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("")
    public String index(ModelMap map) {
        BigDecimal zje=btyElmService.zje();
        map.put("zje",zje);
        return PREFIX + "/btyElm.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyElm_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyElm_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyElmParam btyElmParam) {
        this.btyElmService.add(btyElmParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyElmParam btyElmParam) {
        this.btyElmService.update(btyElmParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyElmParam btyElmParam) {
        this.btyElmService.delete(btyElmParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyElmParam btyElmParam) {
        BtyElm detail = this.btyElmService.getById(btyElmParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-08-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyElmParam btyElmParam) {
        return this.btyElmService.findPageBySpec(btyElmParam);
    }

    /**
     * 导入excel
     */
//    @RequestMapping("/import")
//    @ResponseBody
//    public Object addUser(@RequestParam("file") MultipartFile file) {
//        String fileName = file.getOriginalFilename();
//        Boolean flag;
//        JsonResult jsonResult = new JsonResult();
//        try {
//            flag = btyElmService.batchImport(fileName, file);
//        } catch (Exception e) {
//            e.printStackTrace();
//            jsonResult.setState(0);
//            jsonResult.setErrmsg(e.getMessage());
//            return jsonResult;
//        }
//        return flag;
//    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file) {
        JsonResult jsonResult = new JsonResult();
        //导入之前删掉之前的数据
        elmMapper.delete(new QueryWrapper<>());
        try {
            ElmExcelListener elmExcelListener=new ElmExcelListener(btyElmService);
            EasyExcel.read(file.getInputStream(), BtyElm.class, elmExcelListener).sheet(0).doRead();
            //导入成功之后更新饿了么结算金额到yybb中
            BtyElmParam param=new BtyElmParam();
            List<BtyElmResult>list=elmMapper.elmList(param);
            BigDecimal jsje=BigDecimal.ZERO;
            for (BtyElmResult btyElmResult : list) {
                String zdrq=btyElmResult.getZdrq();
                jsje=btyElmResult.getJsje();
                Integer yybbId=btyElmResult.getYybbId();
                String mdid=btyElmResult.getMdid();
                Yybb yybb=yybbService.getById(yybbId);
                if(ToolUtil.isNotEmpty(yybb)){
                    if(ToolUtil.isNotEmpty(yybb.getElmsj())&&(yybb.getElmsj().compareTo(BigDecimal.ZERO)==1|yybb.getElmsj().compareTo(BigDecimal.ZERO)==-1)){
                        yybb.setElmsj(yybb.getElmsj().add(jsje));
                    }else {
                        yybb.setElmsj(jsje);
                    }
                    Boolean flag=yybbService.updateById(yybb);
                    if(flag==true){
                        List<BtyElm>listElm=elmMapper.selectList(new QueryWrapper<BtyElm>().eq("mdid",mdid));
                        if(listElm.size()>0){
                            BtyElmParam elmParam=new BtyElmParam();
                            elmParam.setMdid(listElm.get(0).getMdid());
                            elmParam.setZdrq(zdrq);
                            elmMapper.updateByMdid(elmParam);
                        }
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

    /**
     * 获取导出excel的数据
     * @param param
     * @return
     */
    @RequestMapping("/listSearchExport")
    @ResponseBody
    public List<BtyElmResult> listSearchExport (BtyElmParam param){
        List<BtyElmResult> list= btyElmService.customList(param);
        return list;
    }

}


